package com.hnote.hnoteapplication.base.utils;

import android.databinding.BindingAdapter;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hnote.hnoteapplication.R;
import com.hnote.hnoteapplication.base.utils.helper.GlideCircleTransform;
import com.hnote.hnoteapplication.base.utils.helper.GlideRoundTransform;


/**
 * =====================================================
 * 功    能： 加载图片
 * 作    者： 加菲猫
 * 邮    箱： pfliu@3box.cc
 * 时    间： 2017/2/15 0015 19:22
 * =====================================================
 */

public class LoadUtils {

    @BindingAdapter({"imageUrl"})
    public static void loadImage(ImageView imageView, String url) {
        if (TextUtils.isEmpty(url)) {
            //如果网址为空, 默认加载ic_launcher
            imageView.setImageResource(R.mipmap.ic_launcher);
        } else {
            //使用Glide加载图片
            Glide.with(imageView.getContext()).load(url).into(imageView);
        }
    }

    @BindingAdapter({"loadHead"})
    public static void loadHead(ImageView imageView, String url) {
        if (TextUtils.isEmpty(url)) {
            //如果网址为空, 默认加载ic_launcher
            imageView.setImageResource(R.mipmap.ic_launcher);
        } else {
            //使用Glide加载图片
            Glide.with(imageView.getContext()).load(url).into(imageView);
                    Glide.with(imageView.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransform(imageView.getContext()))
                .error(R.mipmap.ic_launcher)
                .into(imageView);
        }
    }

    @BindingAdapter({"loadRound"})
    public static void loadRound(ImageView imageView, String url) {
        if (TextUtils.isEmpty(url)) {
            //如果网址为空, 默认加载ic_launcher
            Glide.with(imageView.getContext())
                    .load(R.mipmap.ic_launcher)
                    .transform(new GlideRoundTransform(imageView.getContext(), SystemUtils.px2dip(imageView.getContext(), 25)))
                    .into(imageView);
        } else {
            //使用Glide加载图片
            Glide.with(imageView.getContext())
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .transform(new GlideRoundTransform(imageView.getContext(), SystemUtils.px2dip(imageView.getContext(), 25)))
                    .error(R.mipmap.ic_launcher)
                    .into(imageView);
        }
    }
}
